<template>
  <div class="cadre-management">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-button type="primary" @click="showAddDialog">添加干部+</el-button>
      <div class="search-inputs">
        <el-input v-model="searchParams.name" placeholder="请输入干部姓名" clearable
          style="width: 200px; margin-right: 10px;" />
        <el-input v-model="searchParams.role" placeholder="请输入干部角色" clearable
          style="width: 200px; margin-right: 10px;" />
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </div>
    </div>

    <!-- 干部表格 -->
    <el-table :data="pagedCadres" border style="width: 100%; margin-top: 20px;">
      <el-table-column prop="cadreNumber" label="干部编号" />
      <el-table-column prop="name" label="干部姓名" />
      <el-table-column prop="gender" label="性别">
        <template #default="{ row }">
          {{ row.gender === 'M' ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="电话" />
      <el-table-column prop="idCard" label="身份证号" min-width="150" />
      <el-table-column prop="roleType" label="分配角色" min-width="120" />
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="showEditDialog(row)">修改</el-button>
          <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :total="totalCadre"
        :page-sizes="[5, 10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <!-- 添加/编辑干部对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px" :before-close="handleCloseDialog">
      <el-form :model="cadreForm" label-width="100px">
        <el-form-item label="干部编号" required>
          <el-input v-model="cadreForm.cadreNumber" placeholder="请输入干部编号" :disabled="isEditing" />
        </el-form-item>
        <el-form-item label="干部姓名" required>
          <el-input v-model="cadreForm.name" placeholder="请输入干部姓名" />
        </el-form-item>
        <el-form-item label="性别" required>
          <el-radio-group v-model="cadreForm.gender">
            <el-radio label="M">男</el-radio>
            <el-radio label="F">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话" required>
          <el-input v-model="cadreForm.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="身份证号" required>
          <el-input v-model="cadreForm.idCard" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item label="分配角色" required>
          <el-select v-model="cadreForm.roleType" placeholder="请选择角色" style="width: 100%">
            <el-option v-for="item in roles" :key="item.roleId" :label="item.roleName" :value="item.roleName" />

          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleCloseDialog">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage, ElMessageBox, roleTypes } from 'element-plus'
import { getCadreTables, addCadreTables, updateadreTables, deleteCadreTables } from '@/api/admin'
import { getRoles } from "@/api/role"

const currentPage = ref(1)
const pageSize = ref(10)
const pagedCadres = ref([])
// 条件搜素哦参数
const searchParams = reactive({
  name: '',
  role: '',
  currentPage: currentPage.value,
  pageSize: pageSize.value
})
const totalCadre = ref(0)

const queryCadre = () => {
  getCadreTables(searchParams).then((res) => {
    if (res.code == 2000) {
      pagedCadres.value = res.data
      totalCadre.value = res.total
    } else {
      pagedCadres.value = []
      totalCadre.value = 0
    }
  })
}
queryCadre()

let roles = ref([])
// 弹窗中查询角色
const loadRoles = () => {
  getRoles().then(res => {
    if (res.code == 2000) {
      roles.value = res.data
    }

  })
}
// 搜索参数


// 分页参数



// 对话框相关状态
const dialogVisible = ref(false)
const isEditing = ref(false)
const currentCadreId = ref('')
const dialogTitle = computed(() => isEditing.value ? '修改干部信息' : '添加干部')

// 干部表单
const cadreForm = reactive({
  cadreNumber: '',
  name: '',
  gender: 'M',
  phone: '',
  idCard: '',
  roleType: ''
})

// 显示添加对话框
const showAddDialog = () => {
  loadRoles()
  isEditing.value = false
  resetForm()
  dialogVisible.value = true
}

// 显示编辑对话框
const showEditDialog = (row) => {
  isEditing.value = true
  currentCadreId.value = row.id
  Object.assign(cadreForm, JSON.parse(JSON.stringify(row)))
  dialogVisible.value = true
}

// 重置表单
const resetForm = () => {
  Object.assign(cadreForm, {
    id: '',
    name: '',
    gender: 'male',
    phone: '',
    idCard: '',
    role: ''
  })
}

// 关闭对话框
const handleCloseDialog = () => {
  dialogVisible.value = false
}

// 确认添加/编辑
const handleConfirm = () => {
  // 必填字段验证
  if (!cadreForm.cadreNumber || !cadreForm.name || !cadreForm.phone || !cadreForm.idCard || !cadreForm.roleType) {
    ElMessage.warning('请填写完整信息')
    return
  }



  if (isEditing.value) {
    updateadreTables(cadreForm).then(res => {
      if (res.code == 2000) {
        ElMessage.success('修改成功')
        handleSearch() // 重新筛选数据
      }
    })
  } else {
    addCadreTables(cadreForm).then(res => {
      if (res.code == 2000) {
        ElMessage.success('添加成功')
        handleSearch() // 重新筛选数据
      }
    })
  }

  dialogVisible.value = false
}

// 处理删除
const handleDelete = (row) => {
  ElMessageBox.confirm('是否确认删除该干部?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteCadreTables(row.cadreId).then(res => {
      ElMessage.success('删除成功')
      handleSearch() // 重新筛选数据
    })
  })
}

// 处理搜索
const handleSearch = () => {
  queryCadre()
}

// 处理分页大小变化
const handleSizeChange = (val) => {
  searchParams.pageSize = val
  handleSearch()
}

// 处理当前页变化
const handleCurrentChange = (val) => {
  searchParams.currentPage = val
  handleSearch()
}


</script>

<style scoped>
.cadre-management {
  padding: 20px;
}

.search-area {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.search-inputs {
  display: flex;
  align-items: center;
  margin-left: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* 修改按钮样式 */
.el-button--primary {
  background-color: #409EFF;
  border-color: #409EFF;
  color: white;
}

/* 删除按钮样式 */
.el-button--danger {
  background-color: #F56C6C;
  border-color: #F56C6C;
  color: white;
}
</style>